package coms.action;

import com.opensymphony.xwork2.Action;
import coms.delegates.EventDelegate;
import coms.delegates.ConferenceDelegate;
import coms.dto.Event;
import coms.dto.Conference;
import coms.dto.User;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.JMSException;

public class EventAction extends ComsActionSupport implements IAction {

    private int eventId;
    private Event event;
    private List<Event> eventList;
    private EventDelegate eventDelegate;
    
    private List<Conference> conferenceList;
    private ConferenceDelegate conferenceDelegate;
    
    private String action;
     private boolean admin;

    public boolean isAdmin() {
       User u = (User) getSession().get("user");
        if (u != null) {
            admin = u.isAdmin();
        } 
        return admin;
    }

    public void setAdmin(boolean admin) {
        this.admin = admin;
    }

    public EventAction() {
        
        event = new Event();
                
        eventList = new ArrayList<Event>();
        eventDelegate = new EventDelegate();

        conferenceList = new ArrayList<Conference>();
        conferenceDelegate = new ConferenceDelegate();

    }

    private void initForm() {
        try {
            conferenceList = conferenceDelegate.findAll();
        } catch (Exception ex) {
            Logger.getLogger(EventAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void populateDefault() {
        Date today = Calendar.getInstance().getTime();
        event.setStartDate(today);
        event.setEndDate(today);
    }

    private void populate() {
        try {
            event = eventDelegate.getById(eventId);
        } catch (Exception ex) {
            Logger.getLogger(EventAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public String input() {
        initForm();
        populateDefault();
        action = "createEvent_";
        return "input";
    }

    @Override
    public String edit() {
        initForm();
        populate();
        action = "updateEvent_" + event.getId();
        return "edit";
    }

    @Override
    public String create() {
        try {
            eventDelegate.create(event);
        } catch (JMSException ex) {
            Logger.getLogger(EventAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return Action.SUCCESS;
    }

    @Override
    public String update() {
        try {
            eventDelegate.update(event);
        } catch (Exception ex) {
            Logger.getLogger(EventAction.class.getName()).log(Level.SEVERE, null, ex);
        }

        return "updated";
    }

    @Override
    public String delete() {
        populate();
        eventDelegate.delete(event);
        return "deleted";
    }

    @Override
    public String findAll() {
        try {
            eventList = eventDelegate.findAll();
        } catch (Exception ex) {
            Logger.getLogger(EventAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return Action.SUCCESS;
    }

    @Override
    public String search() {
        initForm();
        try {
                eventList = eventDelegate.search(event);
        } catch (Exception ex) {
            Logger.getLogger(EventAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "search";
    }

    public Event getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }

    public List<Event> getEventList() {
        return eventList;
    }

    public void setEventList(List<Event> eventList) {
        this.eventList = eventList;
    }

    public List<Conference> getConferenceList() {
        return conferenceList;
    }

    public void setConferenceList(List<Conference> conferenceList) {
        this.conferenceList = conferenceList;
    }

    public void setConference(int conferenceId) {
        try {
            Conference conference = conferenceDelegate.getById(conferenceId);
            event.setConference(conference);
        } catch (Exception ex) {
            Logger.getLogger(EventAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getEventId() {
        return "" + eventId;
    }

    public void setEventId(String eventId) {
        try {
            if (eventId != null && !eventId.equals("")) {
                this.eventId = Integer.parseInt(eventId);
                event.setId(this.eventId);
            }
        } catch (Exception ex) {
            Logger.getLogger(EventAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }
}
